from werkzeug.security import generate_password_hash, check_password_hash
from models import User, insert

def get_user_id_by_username(username):
    user = User.query.filter_by(username=username).first()
    if user is None:
        return None
    return user.id

def register_user(username, password):
    if User.query.filter_by(username=username).first():
        return False

    new_user = User()
    new_user.username = username
    new_user.password = generate_password_hash(password)
    insert(new_user)
    return True

def validate_user(username, password):
    user = User.query.filter_by(username=username).first()
    if user is None:
        return False
    if check_password_hash(user.password, password):
        return True
    return False
